package aliyun.cx.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import aliyun.cx.admin.entity.Category;
import aliyun.cx.admin.mapper.CategoryMapper;
import aliyun.cx.admin.service.CategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    
    private final CategoryMapper categoryMapper;
    
    public CategoryServiceImpl(CategoryMapper categoryMapper) {
        this.categoryMapper = categoryMapper;
    }
    
    @Override
    public Page<Category> getCategoryPage(Integer current, Integer size, String name) {
        Page<Category> page = new Page<>(current, size);
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        
        // 如果提供了搜索名称，添加模糊查询条件
        if (name != null && !name.trim().isEmpty()) {
            queryWrapper.like("name", name.trim());
        }
        
        // 查询所有分类（包括已删除的），不添加软删除条件
        // 按创建时间倒序排列
        queryWrapper.orderByDesc("create_time");
        
        return page(page, queryWrapper);
    }
    
    @Override
    public List<Category> getAllCategories() {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0); // 只返回未删除的分类
        return list(queryWrapper);
    }
    
    @Override
    public boolean createCategory(Category category) {
        category.setCreateTime(LocalDateTime.now());
        category.setDeleted(0); // 设置为未删除状态
        return save(category);
    }
    
    @Override
    public boolean updateCategory(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        return updateById(category);
    }
    
    @Override
    public boolean deleteCategory(Long id) {
        return removeById(id);
    }
    
    @Override
    public boolean isNameExists(String name, Long excludeId) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name).eq("deleted", 0); // 只检查未删除的分类
        if (excludeId != null) {
            queryWrapper.ne("id", excludeId);
        }
        return count(queryWrapper) > 0;
    }
    
    @Override
    public boolean isCategoryInUse(Long id) {
        // 检查是否有文章使用该分类
        return categoryMapper.countArticlesByCategoryId(id) > 0;
    }
    
    @Override
    public boolean softDeleteCategory(Long id) {
        Category category = getById(id);
        if (category != null) {
            category.setDeleted(1);
            category.setDeletedTime(LocalDateTime.now());
            return updateById(category);
        }
        return false;
    }
    
    @Override
    @Transactional
    public boolean restoreCategory(Long id) {
        Category category = getById(id);
        if (category != null && category.getDeleted() == 1) {
            category.setDeleted(0);
            category.setDeletedTime(null);
            return updateById(category);
        }
        return false;
    }
    
    @Override
    public Category findDeletedCategoryByName(String name) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name).eq("deleted", 1);
        return getOne(queryWrapper);
    }
}
